package com.example.services;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import com.example.ShoppingCartApiApplication;
import com.example.exceptions.UserAlreadyExistsException;
import com.example.exceptions.UserNotFoundException;
import com.example.models.UserRole;
import com.example.models.Users;
import com.example.repositories.UserRoleRepository;
import com.example.repositories.UsersRepository;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ShoppingCartApiApplication.class)
@WebAppConfiguration
@IntegrationTest({"server.port=9090"})
public class UsersServiceIT {
	@Autowired
	private UsersRepository usersRepository;
	
	@Autowired
	private UsersService usersService;
	
	Users user;
	
	@Before
    public void setup() {
		user = usersRepository.save(new Users("ilan", "ilan", true));
	}
	
	@Test(expected = UserNotFoundException.class) 
	public void loginWithUserNotFound() throws Exception{
		Users userNotRegistered = new Users("somebody", "pass", true);
		usersService.login(userNotRegistered);
	}
	
	@Test
	public void login() throws Exception{
		usersService.login(user);
	}
	
	@Test(expected = UserAlreadyExistsException.class) 
	public void registerUserWithUsernameAlreadyExistent() throws Exception{
		usersService.register(user);
	}
	
	@Test
	public void registerUser() throws Exception{
		Users userNotRegistered = new Users("somebody", "pass", true);
		usersService.register(userNotRegistered);
	}
}
